package Servlet;

import Utility.Utility;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "add", value = "/add")
public class Add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        String username = req.getParameter("name");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String address = req.getParameter("address");

        System.out.println(username+","+sex+","+age+","+address);

        String sql = "insert into student values(null,?,?,?,?)";
        int i = Utility.add(sql,username,age,sex,address);

        if(i>0){
            out.write("添加成功！");
        }else{
            out.write("添加失败！");
        }

        out.write("<a href=\"http://localhost:8080/stu\">返回</a>");

    }
}
